package com.sakura.conf;/**
 * Created by apple on 2019/3/25.
 */

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author jixd
 * @Description TODO
 * @Date 2019/3/25 2:31 PM
 * @Version 1.0
 **/
public class DBManager {
    private static Configuration configuration = new Configuration();

    public static final String PROPERTY_FILE_NAME =  "application.properties";

    static {
        Properties properties = new Properties();
        try {
            properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(PROPERTY_FILE_NAME));
        } catch (IOException e) {
            e.printStackTrace();
        }

        configuration.setDriver(properties.getProperty("driver"));
        configuration.setPoPackage(properties.getProperty("poPackage"));
        configuration.setPwd(properties.getProperty("pwd"));
        configuration.setSrcPath(properties.getProperty("srcPath"));
        configuration.setUrl(properties.getProperty("dataSourceUrl"));
        configuration.setUser(properties.getProperty("user"));
        configuration.setUsingDB(properties.getProperty("usingDB"));
    }

    public static Connection getConn(){
        try {
            Class.forName(configuration.getDriver());
            return DriverManager.getConnection(configuration.getUrl(), configuration.getUser(), configuration.getPwd());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static Configuration getConfiguration(){
        return configuration;
    }
}
